﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Data;
using System.Web.UI.WebControls;

namespace GS.ManagerPage
{
    public class PagingPage : EntitesClass
    {
        public int curPage, TotalPages, TotalPageSet, CurrentSet, PageSetSize, SetLBound, SetUBound, IndustryID, TotalRecords;
        public String PgActiveFont, PgActiveLink_o, PgActiveLink_c, FileName, Parameters;
        public PagingPage(string FileName, string Parameters)
            : base()
        {
            this.Parameters = Parameters;
            this.FileName = FileName;
        }

        public string MovePreviousSet()
        {
            if (CurrentSet > 1)
            {
                if (Parameters != string.Empty)
                    return string.Format("<table cellpadding=\"0\" style=\"margin-left:auto\" cellspacing=\"0\"><tr><td>{0}</td>", WriteImageLink(FileName, string.Format("{0}&cp={1}", Parameters, curPage - PageSetSize), string.Format("{0}/ico_previousset_on.gif", Config.Config.Images), ""));
                else
                    return string.Format("<td>{0}</td>", WriteImageLink(FileName, string.Format("cp={0}", curPage - PageSetSize), string.Format("{0}/ico_previousset_on.gif", Config.Config.Images), ""));
            }
            else
                return string.Format("<table cellpadding=\"0\" style=\"margin-left:auto\" cellspacing=\"0\"><td>{0}</td>", WriteImage(string.Format("{0}/ico_previousset_off.gif", Config.Config.Images), ""));
        }

        public string MovePreviousPage()
        {
            if (curPage > 1)
            {
                if (Parameters != string.Empty)
                    return string.Format("<td>{0}</td>", WriteImageLink(FileName, string.Format("{0}&cp={1}", Parameters, curPage - 1), string.Format("{0}/ico_previouspage_on.gif", Config.Config.Images), ""));
                else
                    return string.Format("<td>{0}</td>", WriteImageLink(FileName, string.Format("cp={0}", curPage - 1), string.Format("{0}/ico_previouspage_on.gif", Config.Config.Images), ""));
            }
            else
                return string.Format("<td>{0}</td>", WriteImage(string.Format("{0}/ico_previouspage_off.gif", Config.Config.Images), ""));
        }

        public string MoveNextPage()
        {
            if (curPage < TotalPages)
            {
                if (Parameters != string.Empty)
                    return string.Format("<td>{0}</td>", WriteImageLink(FileName, string.Format("{0}&cp={1}", Parameters, curPage + 1), string.Format("{0}/ico_nextpage_on.gif", Config.Config.Images), ""));
                else
                    return string.Format("<td>{0}</td>", WriteImageLink(FileName, string.Format("cp={0}", curPage + 1), string.Format("{0}/ico_nextpage_on.gif", Config.Config.Images), ""));
            }
            else
                return string.Format("<td>{0}</td>", WriteImage(string.Format("{0}/ico_nextpage_off.gif", Config.Config.Images), ""));
        }

        public string MoveNextSet()
        {
            if (CurrentSet < TotalPageSet)
            {
                if (Parameters != string.Empty)
                    return string.Format("<td>{0}</td></tr></table>", WriteImageLink(FileName, string.Format("{0}&cp={1}", Parameters, curPage + PageSetSize), string.Format("{0}/ico_nextset_on.gif", Config.Config.Images), ""));
                else
                    return string.Format("<td>{0}</td></tr></table>", WriteImageLink(FileName, string.Format("cp={0}", curPage + PageSetSize), string.Format("{0}/ico_nextset_on.gif", Config.Config.Images), ""));
            }
            else
                return string.Format("<td>{0}</td></tr></table>", WriteImage(string.Format("{0}/ico_nextset_off.gif", Config.Config.Images), ""));
        }

        public string WriteCurrentPage()
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            for (int j = SetLBound; j <= SetUBound; j++)
            {
                if (j > TotalPages)
                {
                    continue;
                }
                else
                {
                    if (j == curPage)
                    {
                        PgActiveFont = "class=\"PagingNavTxtOnSelected\"";
                        PgActiveLink_o = "";
                        PgActiveLink_c = "";
                    }
                    else
                    {
                        PgActiveFont = "class=\"PagingNavTxtOff\"";
                        PgActiveLink_o = string.Format("<a href=\"{0}?{1}\">", FileName, Parameters != string.Empty ? string.Format("{0}&cp={1}", Parameters, j) : string.Format("cp={0}", j)); ;
                        PgActiveLink_c = "</a>";
                    }
                }
                sb.Append(string.Format("<td width=\"20px\" align=\"center\" valign=\"middle\" {0}>{1}{2}{3}</td>", PgActiveFont, PgActiveLink_o, j, PgActiveLink_c));
            }
            return sb.ToString();
        }

        public string WriteFullNav()
        {
            //base.GetGuid();
            return string.Format("{0}{1}{2}{3}{4}", MovePreviousSet(), MovePreviousPage(), WriteCurrentPage(), MoveNextPage(), MoveNextSet());
        }
        public void SetData(INamingContainer Container, DataSet ds)
        {
            if (Container is Repeater)
            {
                ((Repeater)Container).DataSource = ds.Tables[0];
                ((Repeater)Container).DataBind();
            }

            if (Container is DataList)
            {
                ((DataList)Container).DataSource = ds.Tables[0];
                ((DataList)Container).DataBind();
            }

            TotalPages = (int)ds.Tables[1].Rows[0][0];
            TotalPageSet = (int)ds.Tables[1].Rows[0][1];
            CurrentSet = (int)ds.Tables[1].Rows[0][2];
            PageSetSize = (int)ds.Tables[1].Rows[0][3];
            SetLBound = (int)ds.Tables[1].Rows[0][4];
            SetUBound = (int)ds.Tables[1].Rows[0][5];
            TotalRecords = (int)ds.Tables[1].Rows[0][6];
        }
        public void SetCurPage(string curPage)
        {
            this.curPage = int.Parse(string.IsNullOrEmpty(curPage) ? "1" : curPage);

        }

        public void SetPagingInfo(DataTable dtPagingInfo)
        {
            TotalPages = (int)dtPagingInfo.Rows[0][0];
            TotalPageSet = (int)dtPagingInfo.Rows[0][1];
            CurrentSet = (int)dtPagingInfo.Rows[0][2];
            PageSetSize = (int)dtPagingInfo.Rows[0][3];
            SetLBound = (int)dtPagingInfo.Rows[0][4];
            SetUBound = (int)dtPagingInfo.Rows[0][5];
            TotalRecords = (int)dtPagingInfo.Rows[0][6];
        }
    }
}
